<Project>

  <PropertyGroup>
    <EnableLibraryImportGenerator Condition="'$(EnableLibraryImportGenerator)' == '' and
                                             '$(MSBuildProjectName)' == 'System.Private.CoreLib'">true</EnableLibraryImportGenerator>
  </PropertyGroup>

  <ItemGroup>
    <EnabledGenerators Include="LibraryImportGenerator" Condition="'$(EnableLibraryImportGenerator)' == 'true'" />
    <!-- If the current project is not System.Private.CoreLib, we enable the LibraryImportGenerator source generator
         when the project is a C# source or test project that:
         - doesn't target the a TFM that includes LibraryImportGenerator or
         - doesn't reference the live targeting pack (i.e. when inbox) and
           - references System.Private.CoreLib, or
           - references System.Runtime.InteropServices -->
    <EnabledGenerators Include="LibraryImportGenerator"
                       Condition="'$(EnableLibraryImportGenerator)' == '' and
                                  (
                                    '$(IsSourceProject)' == 'true' or
                                    '$(IsTestProject)' == 'true' or
                                    '$(IsTestSupportProject)' == 'true'
                                  ) and
                                  '$(MSBuildProjectExtension)' == '.csproj' and
                                  (
                                    !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0')) or
                                    (
                                      '$(DisableImplicitFrameworkReferences)' == 'true' and
                                      (
                                        '@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true' or
                                        '@(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))' == 'true'
                                      )
                                    )
                                  )" />
    <!-- We enable the ComInterfaceGenerator source generator
         when the project is a C# source project that:
         - references System.Runtime.InteropServices directly and not through the live targeting pack (i.e. when inbox) -->
    <EnabledGenerators Include="ComInterfaceGenerator"
                       Condition="'$(IsSourceProject)' == 'true' and
                                  '$(MSBuildProjectExtension)' == '.csproj' and
                                  (
                                      '$(DisableImplicitFrameworkReferences)' == 'true' and
                                      '@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true'
                                  )" />
  </ItemGroup>

  <ItemGroup Condition="'@(EnabledGenerators)' != '' and
                        @(EnabledGenerators->AnyHaveMetadataValue('Identity', 'LibraryImportGenerator')) and
                        !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
    <Compile Include="$(CoreLibSharedDir)System\Runtime\InteropServices\LibraryImportAttribute.cs" />
    <Compile Include="$(CoreLibSharedDir)System\Runtime\InteropServices\StringMarshalling.cs" />
  </ItemGroup>

  <!-- Use this complex item list based filtering to add the ProjectReference to make sure dotnet/runtime stays compatible with NuGet Static Graph Restore.
       That is required as the EnabledGenerators condition checks on the Reference and ProjectReference items and hence can't be a property condition. -->
  <ItemGroup Condition="'@(EnabledGenerators)' != ''">
    <ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\Microsoft.Interop.SourceGeneration\Microsoft.Interop.SourceGeneration.csproj"
                      ReferenceOutputAssembly="false"
                      OutputItemType="Analyzer"
                      SetConfiguration="Configuration=$(LibrariesConfiguration)" />
    <ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\LibraryImportGenerator\LibraryImportGenerator.csproj"
                      ReferenceOutputAssembly="false"
                      OutputItemType="Analyzer"
                      SetConfiguration="Configuration=$(LibrariesConfiguration)"
                      Condition="@(EnabledGenerators->AnyHaveMetadataValue('Identity', 'LibraryImportGenerator'))" />
    <ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\ComInterfaceGenerator\ComInterfaceGenerator.csproj"
                      ReferenceOutputAssembly="false"
                      OutputItemType="Analyzer"
                      SetConfiguration="Configuration=$(LibrariesConfiguration)"
                      Condition="@(EnabledGenerators->AnyHaveMetadataValue('Identity', 'ComInterfaceGenerator'))" />
  </ItemGroup>

  <Target Name="ConfigureGenerators"
          DependsOnTargets="ConfigureLibraryImportGenerator"
          BeforeTargets="CoreCompile" />

  <!-- Microsoft.Interop.LibraryImportGenerator -->
  <Target Name="ConfigureLibraryImportGenerator"
          Condition="'@(EnabledGenerators)' != '' and @(EnabledGenerators->AnyHaveMetadataValue('Identity', 'LibraryImportGenerator'))"
          DependsOnTargets="ResolveProjectReferences"
          BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun">
    <PropertyGroup>
        <LibraryImportGenerator_UseMarshalType>true</LibraryImportGenerator_UseMarshalType>
    </PropertyGroup>
  </Target>

  <Import Project="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator.props" />

</Project>
